clear all
set more off

global path "~/shared_space/dbaharc/BHOR-YugoslavianRefugees/"
qui do "${path}do_files/InitializeGlobals.do"

//net install http://www.stata.com/users/vwiggins/grc1leg.pkg
//ssc install xtpqml

/*** GRAPHS WITH OVERALL STORY ***/
use ~/shared_space/dbaharc/General_Data/Madisson/mpd2018.dta , clear
twoway (line pop year if countrycode=="YUG", lw(thick)) if year>=1990 & year<=2005, scheme(s1color) ///
        xtick(1990(5)2005) xscale(range(1990 2005)) ylabel(,grid)
graph export "${path}graphs/YugoslaviaPopulation.eps", as(eps) replace

//B&W Version
#d ;
twoway 
	(line pop year if countrycode=="YUG", lw(thick) lcol(gs8)) 
	/*next, create an empty line which creates a legend and a second axis*/
	(line pop year if countrycode=="YUG", lw(none) yaxis(2)) 
		if year>=1990 & year<=2005 , 
		scheme(s1mono)
		ytitle("In 1000 persons", size(medsmall))
		ytitle(" ", axis(2)) 
		xtitle("Year")
		ylabel(, format(%6.0fc))
		ylabel(, labcol(gs16) axis(2))	
		ylabel(, noticks axis(2))
		ylabel(, grid)
		xlabel(1990(5)2005) 
		legend(order(1 "Population"))
		/*legend(region(lwidth(none)))*/	
		legend(row(1))
;
#d cr
graph export "${path}graphs/YugoslaviaPopulation_BW.eps", as(eps) replace


/*Similiarity Index*/
use "${path}data/YUGSimilarityIndex.dta", clear
g temp = iso3_d if iso3_o=="YUG"
replace iso3_d = "YUG"
replace iso3_o = temp if !mi(temp)
egen countryid = group(iso3_o)
areg simindex i.year, a(countryid)
predict simindex_dt, res

twoway  (qfitci simindex year)  (scatter simindex year) if year>=1984 & year<2015 & iso3_o == "DEU" & iso3_d=="YUG", scheme(s1color) legend(off) xtitle(Year) ytitle(BHH Exoprt Similarity Index)
graph export "${path}graphs/SimIndexYUGDEU.eps", as(eps) replace

use "${path}data/ah100d01_mig_pop_fyug.dta", replace
g mig_netflow = mig_inflow - mig_outflow
replace mig_asyl = . if mig_asyl==0
replace mig_netflow = mig_netflow/1000
replace mig_asyl    = mig_asyl   /1000
replace mig_stock   = mig_stock  /1000

twoway (bar mig_netflow year, lcolor(black) lwidth(vthin) fintensity(25)) ///
       (scatter mig_asyl year, msymbol(x) msize(small)) ///
       (line mig_stock year, lw(thick) yaxis(2)) ///
        if year>=1980 & year<=2010, ///
           scheme(s1color) ytitle("Net inflow / Asylum requests") ytitle("Stock", axis(2)) ///
             xlabel(1980(5)2010) ylabel(,labsize(small)) ylabel(,labsize(small) axis(2)) ///
                    legend(order(1 "Net inflow" 2 "Asylum requests" 3 "Stock") cols(3))
graph export "${path}graphs/YUGFlowsStock.eps", as(eps) replace

//B&W Version
#d ;
twoway 
	(bar mig_netflow year, lcolor(black) lwidth(vthin) fintensity(25)) 
	(scatter mig_asyl year, msymbol(x) msize(small)) 
	(line mig_stock year, lw(thick) yaxis(2)) 
		if year>=1980 & year<=2010 , 
			scheme(s1mono) 
			ytitle("In 1000 persons", size(medsmall))
			ytitle("In 1000 persons", size(medsmall) axis(2))
			xtitle("Year")
			ylabel(, format(%6.0fc))
			ylabel(, format(%6.0fc) axis(2)) 
			ylabel(, grid)
            xlabel(1980(5)2010) 
			legend(order(1 "Net inflow" 2 "Asylum requests" 3 "Stock (right axis)"))
			/*legend(region(lwidth(none)))*/
			legend(row(1))
;
#d cr
graph export "${path}graphs/YUGFlowsStock_BW.pdf",  replace


twoway (line mig_emp_total year, lw(thick)) (line mig_emp_trade year, lp(dash) lw(thick)) ///
        if year>=1980 & year<=2010, scheme(s1color) ytitle("Workers") ylabel(,labsize(small))  ///
                    legend(order(1 "Total workers" 2 "Workers in tradables") cols(2))
graph export "${path}graphs/YUGWorkers.eps", as(eps) replace

use "${path}data/desc_fyug_narrative.dta", clear
egen N_arrv_total = total(N_arrv), by(year)
egen N_exit_total = total(N_exit), by(year)
g share_arrv = N_arrv/N_arrv_total
g share_exit = N_exit/N_exit_total

graph bar share_arrv share_exit if nation2=="FYUG" & year>=1985 & year<=2010, scheme(s1color) ///
    over(year, label(angle(vertical) labsize(small))) legend(order(1 "Entry" 2 "Exit")) ///
     ytitle("Share of all foreign workers") bar(2, fintensity(inten50))
graph export "${path}graphs/FYUGEntryExitLaborForce.eps", replace as(eps)

//B&W
graph bar share_arrv share_exit if nation2=="FYUG" & year>=1985 & year<=2010, scheme(s1mono) ///
    over(year, label(angle(vertical) labsize(small))) legend(order(1 "Entry" 2 "Exit")) ///
     ytitle("Share of all foreign workers") bar(2, fintensity(inten50))
graph export "${path}graphs/FYUGEntryExitLaborForce_BW.eps", replace as(eps)


use "${path}data/ExportsYUGCountriesROW.dta", clear
replace exp = exp/1000000000
reshape wide exp, i(year) j(iso3) string
graph bar exp*, over(year, label(angle(90) labsize(small))) stack scheme(s1color) ///
      legend(order(1 "BIH" 2 "HRV" 3 "MKD" 4 "MNE" 5 "SCG" 6 "SRB" 7 "SVN" 8 "YUG") cols(4)) ///
      ytitle("Total Exports to ROW, in billions US$")
graph export "${path}graphs/ExportsYUGtoROW.eps", as(eps) replace

egen expYUGnoSVN = rowtotal(expBIH expHRV expMKD expMNE expSCG expSRB expYUG)
egen expYUGtot = rowtotal(expBIH expHRV expMKD expMNE expSCG expSRB expSVN expYUG)
twoway (line expYUGtot year if year<=1991, lw(thick)) ///
       (line expYUGtot year if year>=1991) ///
       (line expYUGnoSVN year if year>=1991, lp(dash)), scheme(s1color) ///
       legend(order(1 "YUG" 3 "Ex-YUG w/o SVN" 2 "Ex-YUG with SVN") cols(3)) ///
       ytitle("Total Exports to ROW, in billions US$")
graph export "${path}graphs/ExportsYUGSVN.eps", as(eps) replace

/********************/
/*** Prepare Data ***/
/********************/
use "${path}data/GermanYUGMigration.dta", clear
qui do "${path}do_files/InitializeVars.do"
keep if iso3=="YUG"
xtset product year

label var lnexp "$ log(exp) $"
label var lnexpplus1 "$ log(exp+1) $"
label var asinhexp "$ asinh(exp) $"

//Create 1-digit product categories
cap g product_1d = int(product/1000)
label define onedigit_lbl 0 "Food & live animals" ///
  1 "Beverages & tobacco" ///
  2 "Crude materials, inedible, except fuels" ///
  3 "Mineral fuels, lubricants & related materials" ///
  4 "Animal and vegetable oils, fats & waxes" ///
  5 "Chemical and related products, n.e.s." ///
  6 "Manufactured goods classified by material" ///
  7 "Machinery & transport equipment" ///
  8 "Miscellaneous manufactured articles" ///
  9 "Commodities & transactions not classified", replace

/**************************************/
/*Exogeneity of exit across industries*/
/**************************************/
preserve
egen total_arrived = total(mig_rm_came_sam_9195), by(year)
egen total_left = total(mig_rm_left_sam_2000), by(year)
g share_arrived = mig_rm_came_sam_9195/total_arrived
g share_left = mig_rm_left_sam_2000/total_left
pwcorr share_* if year==1990, sig

twoway (scatter share_arrived share_left, msymbol(none) mlabel(product)) ///
       (function y=x, range(0 0.05) lp(dash)) if year==1990, scheme(s1color) ///
       xtitle("Share by industry of Yugoslavian arrivals in 91-95") ///
       ytitle("Share by industry of Yugoslavian returnees by 2000") legend(off)
graph export "${path}graphs/ShareArrivalReturneesByProduct.eps", replace as(eps)


twoway (scatter share_arrived share_left if product_1d==0, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==1, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==2, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==3, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==4, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==5, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==6, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==7, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==8, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==9, mlabel(product)) ///
       (function y=x, range(0 0.05) lp(dash) lc(black)) if year==1990, scheme(s1color) ///
       xtitle("Share by industry of Yugoslavian arrivals in 91-95") ///
       ytitle("Share by industry of Yugoslavian returnees by 2000") ///
       legend(order(1 "Food & live animals" ///
                    2 "Beverages & tobacco" ///
                    3 "Crude materials, inedible, except fuels" ///
                    4 "Mineral fuels, lubricants & related materials" ///
                    5 "Animal and vegetable oils, fats & waxes" ///
                    6 "Chemical and related products, n.e.s." ///
                    7 "Manufactured goods classified by material" ///
                    8 "Machinery & transport equipment" ///
                    9 "Miscellaneous manufactured articles" ///
                    10 "Commodities & transactions not classified") cols(2) size(tiny) symx(*.25) symy(*.25) keygap(*.5)) ysize(8)
graph export "${path}graphs/ShareArrivalReturneesByProduct_1D.eps", replace as(eps)

//B&W
twoway (scatter share_arrived share_left if product_1d==0, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==1, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==2, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==3, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==4, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==5, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==6, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==7, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==8, mlabel(product)) ///
       (scatter share_arrived share_left if product_1d==9, mlabel(product)) ///
       (function y=x, range(0 0.05) lp(dash) lc(black)) if year==1990, scheme(s1mono) ///
       xtitle("Share by industry of Yugoslavian arrivals in 91-95") ///
       ytitle("Share by industry of Yugoslavian returnees by 2000") ///
       legend(order(1 "Food & live animals" ///
                    2 "Beverages & tobacco" ///
                    3 "Crude materials, inedible, except fuels" ///
                    4 "Mineral fuels, lubricants & related materials" ///
                    5 "Animal and vegetable oils, fats & waxes" ///
                    6 "Chemical and related products, n.e.s." ///
                    7 "Manufactured goods classified by material" ///
                    8 "Machinery & transport equipment" ///
                    9 "Miscellaneous manufactured articles" ///
                    10 "Commodities & transactions not classified") cols(2) size(tiny) symx(*.25) symy(*.25) keygap(*.5)) ysize(8)
graph export "${path}graphs/ShareArrivalReturneesByProduct_1D_BW.eps", replace as(eps)

g diff_share = share_left-share_arrived
foreach var in lnexp lnexpplus1 asinhexp {
    g diff_`var' = F15.`var'-`var'
    eststo, title(`v'): qui reg diff_`var' diff_share if year==1990, cluster(product)
}
estout, $estout_params_txt 
estout using "${path}tables/DiffShareVsExpGrowth.tex", replace $estout_params $estout_pre $estout_post
eststo clear
restore

/*************************/
/* IV First Stage Graphs */
/*************************/
reg treat2000 treatIV if year==1990, cluster(product)
//reg treat2000 treatIV germanexpshare1990 if year==1990, cluster(product)

twoway (scatter treat2000 treatIV, mlabel(product) msymbol(none)) ///
       (lfit treat2000 treatIV, lp(dash)) if year==1995, scheme(s1color) ///
       ytitle("Treatment") xtitle("Treatment IV") legend(off)
graph export "${path}graphs/FirstStageIVDiff.eps", as(eps) replace

twoway (scatter treat2000 treatIV if product_1d==0, mlabel(product)) ///
       (scatter treat2000 treatIV if product_1d==1, mlabel(product)) ///
       (scatter treat2000 treatIV if product_1d==2, mlabel(product)) ///
       (scatter treat2000 treatIV if product_1d==3, mlabel(product)) ///
       (scatter treat2000 treatIV if product_1d==4, mlabel(product)) ///
       (scatter treat2000 treatIV if product_1d==5, mlabel(product)) ///
       (scatter treat2000 treatIV if product_1d==6, mlabel(product)) ///
       (scatter treat2000 treatIV if product_1d==7, mlabel(product)) ///
       (scatter treat2000 treatIV if product_1d==8, mlabel(product)) ///
       (scatter treat2000 treatIV if product_1d==9, mlabel(product)) ///
       (lfit treat2000 treatIV, lp(dash) lc(black)) if year==1995, scheme(s1color) ///
       ytitle("Treatment") xtitle("Treatment IV") ///
       legend(order(1 "Food & live animals" ///
                    2 "Beverages & tobacco" ///
                    3 "Crude materials, inedible, except fuels" ///
                    4 "Mineral fuels, lubricants & related materials" ///
                    5 "Animal and vegetable oils, fats & waxes" ///
                    6 "Chemical and related products, n.e.s." ///
                    7 "Manufactured goods classified by material" ///
                    8 "Machinery & transport equipment" ///
                    9 "Miscellaneous manufactured articles" ///
                    10 "Commodities & transactions not classified") cols(2) size(tiny) symx(*.25) symy(*.25) keygap(*.5)) ysize(8)
graph export "${path}graphs/FirstStageIVDiff_1D.eps", as(eps) replace

// First stage and reduced form estimates
preserve
egen temp1 = total($expdv) if year>=1988 & year<=1990, by(product)
egen temp2 = total($expdv) if year>=2005 & year<=2007, by(product)
g cumexp = temp1/3 if year==1990
replace cumexp = temp2/3 if year==2005
replace lnexp = log(cumexp)
replace lnexpplus1 = log(cumexp+1)
replace asinhexp = asinh(cumexp)
drop temp1 temp2 cumexp
g treatafter = treat2000*$after
eststo, title("treatXafter"): qui reghdfe treatafter c.treatIV#c.$after  if didsample19902005 & !mi(lnexp), $rhsoptions
eststo, title("treatXafter"): qui reghdfe treatafter c.treatIV#c.$after  if didsample19902005, $rhsoptions
foreach v in lnexp lnexpplus1 asinhexp {
    //eststo, title(`v'): qui reghdfe `v' c.treatIV#c.$after  if didsample19902005, $rhsoptions
}
estout, $estout_params_txt 
estout using "${path}tables/DIDFirstStage.tex", replace $estout_params $estout_pre $estout_post
eststo clear
restore

/********************/
/*Summary Statistics*/
/********************/
foreach y in 1990 1995 2000 2005 2010 {
    cap g exp_xdeu`y'M = exp_xdeu`y'/1000000
    //cap g exppc_xdeu`y' = exppc_xdeu`y'
    label var exp_xdeu`y'M "Exports YUG in `y', million USD"
    //label var exppc_xdeu`y' "Exports YUG in `y', USD"
}
label var mig_rm_left_sam_2000 "YUG workers in 91-95 \& out by 2000"
label var mig_rm_came_sam_9195 "YUG workers in 91-95"
global sumstats_start \begin{threeparttable}[b] \begin{tabular}{l c c c c c}\hline\hline \bf Variable & \bf N & \bf Mean & \bf sd & \bf Min & \bf Max \\ \hline
global sumstats_end \hline\end{tabular} \begin{tablenotes} \item {\footnotesize \tablenote} \end{tablenotes} \end{threeparttable}
global sumstatvars exp_xdeu${y1}M exp_xdeu${y2}M mig_rm_came_sam_9195 mig_rm_left_sam_2000 
/* open output file */
cap file close fh
file open fh using "${path}/tables/SumStatsDIDYUG.tex", write replace
/* write table header to file */
file write fh "$sumstats_start" _n
foreach i of global sumstatvars {
    qui su `i' if year==1995
    local obs = `r(N)'
    local mean = `r(mean)'
    local se = `r(sd)'
    local min = `r(min)'
    local max = `r(max)'
    local i = subinstr("`i'","L10.","",.)
    local varlabel: var label `i'
    file write fh "`varlabel'" "&" %9.0fc (`obs')  "&" %14.3fc (`mean')  "&" %14.2fc (`se')  "&" %14.1fc (`min')  "&" %14.1fc (`max') "\\"  _n
}
file write fh "$sumstats_end" _n
/* close file handle */
file close fh
label var fdi ""

/**************/
/* Main Specs */
/**************/
/*Main specification cummulative exports, 3 years*/
preserve
local y1min2 = $y1-2
local y2plus2 = $y2+2
egen temp1 = total($expdv) if year>=`y1min2' & year<=$y1, by(product)
egen temp2 = total($expdv) if year>=$y2 & year<=`y2plus2', by(product)
g cumexp = temp1/3 if year==$y1
replace cumexp = temp2/3 if year==$y2
drop temp1 temp2
replace lnexp = log(cumexp)
replace lnexpplus1 = log(cumexp+1)
replace asinhexp = asinh(cumexp)
foreach v in lnexp lnexpplus1 asinhexp {
    local lbl`v' : variable label `v'
    eststo, title(`lbl`v''): qui reghdfe `v' c.treat2000#c.$after  if $didsample, $rhsoptions
}
foreach v in lnexp lnexpplus1 asinhexp {
    eststo, title(`lbl`v''): qui reghdfe `v'  (c.treat2000#c.$after=c.treatIV#c.$after) if $didsample, $rhsoptions
}
estout, $estout_params_IV_txt mgroups("OLS" "2SLS", $estout_mgroups_ops) 
estout using "${path}tables/DIDMainCumExp.tex", replace $estout_params_IV $estout_pre $estout_post mgroups("OLS" "2SLS", $estout_mgroups_ops) 
eststo clear
restore

//Main results and first stage together
preserve
local y1min2 = $y1-2
local y2plus2 = $y2+2
egen temp1 = total($expdv) if year>=`y1min2' & year<=$y1, by(product)
egen temp2 = total($expdv) if year>=$y2 & year<=`y2plus2', by(product)
g cumexp = temp1/3 if year==$y1
replace cumexp = temp2/3 if year==$y2
drop temp1 temp2
replace lnexp = log(cumexp)
replace lnexpplus1 = log(cumexp+1)
replace asinhexp = asinh(cumexp)
foreach v in lnexp lnexpplus1 asinhexp {
    local lbl`v' : variable label `v'
    eststo, title(`lbl`v''): qui reghdfe `v' c.treat2000#c.$after  if $didsample, $rhsoptions
}
foreach v in lnexp lnexpplus1 asinhexp {
    eststo, title(`lbl`v''): qui reghdfe `v'  (c.treat2000#c.$after=c.treatIV#c.$after) if $didsample, $rhsoptions
}
estout, $estout_params_IV_txt mgroups("OLS" "2SLS", $estout_mgroups_ops) 
estout using "${path}tables/DIDMainCumExp_withFirstStage.tex", replace $estout_params_IV $estout_pre_panelA mgroups("OLS" "2SLS", $estout_mgroups_ops) 
eststo clear
restore

preserve
egen temp1 = total($expdv) if year>=1988 & year<=1990, by(product)
egen temp2 = total($expdv) if year>=2005 & year<=2007, by(product)
g cumexp = temp1/3 if year==1990
replace cumexp = temp2/3 if year==2005
replace lnexp = log(cumexp)
replace lnexpplus1 = log(cumexp+1)
replace asinhexp = asinh(cumexp)
drop temp1 temp2 cumexp
g treatafter = treat2000*$after
foreach v in lnexp lnexpplus1 asinhexp {
    eststo, title("treatXafter"): qui reghdfe treatafter c.treatIV#c.$after if $didsample & !mi(`v'), $rhsoptions
}
estout, $estout_params_txt
*estout using "${path}tables/DIDMainCumExp_withFirstStage.tex", replace $estout_params_IV $estout_pre_panelB $estout_post  
local estout_params = subinstr(`"$estout_params"', "posthead(\hline)", "",.)  
estout using "${path}tables/DIDMainCumExp_withFirstStage.tex", append `estout_params' $estout_pre_panelB $estout_post extracol(0 0 0) mlab(none)
eststo clear
restore

//Computing the effect in terms of % of exports
su $expdv if year==1990
su $expdv if year==2000
local exp1 = r(mean)
su $expdv if year==2005
local exp2 = r(mean)

//2 people (10% of treatment) correspond to 1% increase of exports. That's 200K in 1990, and also nearly 200K for 2000 (exports didnt change much comparing 1990 to 2000).
//17,000 people corresponds then to 

//Export shares
preserve
local y1min2 = $y1-2
local y2plus2 = $y2+2
egen temp1 = total(expshare_xdeu) if year>=`y1min2' & year<=$y1, by(product)
egen temp2 = total(expshare_xdeu) if year>=$y2 & year<=`y2plus2', by(product)
g cumexp = temp1/3 if year==$y1
replace cumexp = temp2/3 if year==$y2
drop temp1 temp2
//replace lnexp = log(cumexp)
//replace lnexpplus1 = log(cumexp+1)
//replace asinhexp = asinh(cumexp)
eststo, title(OLS): qui reghdfe cumexp c.treat2000#c.$after  if $didsample, $rhsoptions
eststo, title(OLS): qui reghdfe cumexp c.treat2000#c.$after  if $didsample & exp_xdeu1990>0, $rhsoptions
eststo, title(OLS): qui reghdfe cumexp c.treat2000#c.$after  if $didsample & exp_xdeu1990==0, $rhsoptions
eststo, title(2SLS): qui reghdfe cumexp  (c.treat2000#c.$after=c.treatIV#c.$after) if $didsample, $rhsoptions
eststo, title(2SLS): qui reghdfe cumexp  (c.treat2000#c.$after=c.treatIV#c.$after) if $didsample & exp_xdeu1990>0, $rhsoptions
eststo, title(2SLS): qui reghdfe cumexp  (c.treat2000#c.$after=c.treatIV#c.$after) if $didsample & exp_xdeu1990==0, $rhsoptions
estout, $estout_params_IV_txt
//estout using "${path}tables/DIDMainCumExp.tex", replace $estout_params_IV $estout_pre $estout_post mgroups("OLS" "2SLS", $estout_mgroups_ops) 
eststo clear
restore

/*5 year periods*/
cap drop *_5y*
cap drop treatbin
local startyear = 1985
g period_5y = floor((year-`startyear')/5)+1 if year>=`startyear' & year<=2014
replace period_5y = . if year<`startyear'
egen insample_5y = tag(product period_5y) if !mi(period_5y)

egen exp_5y = total($expdv), by(period_5y product)
//egen exp_4y = total($expdv), by(period_4y product)
egen fdi_5y = total(fdi), by(period_5y product)
//egen fdi_4y = total(fdi), by(period_4y product)

//xi i.period_5y i.period_4y, noomit
xi i.period_5y, noomit

qui ds _Iperiod_5y*, v(32)
local vlist = r(varlist)
foreach v of local vlist {
    qui su year if `v'==1
    local min = r(min)
    local max = r(max)
    local maxmin = `max'-`min'
    label var `v' "Period `min'-`max'"
    replace exp_5y = exp_5y/`maxmin' if `v'==1
    replace fdi_5y = fdi_5y/`maxmin' if `v'==1
}

g lnexpplus1_5y = log(exp_5y+1)
g lnexp_5y = log(exp_5y)
g asinhexp_5y = asinh(exp_5y)
g lnfdi_5y = asinh(fdi_5y)

local i = 5 //i is the length of the periods, set to 5
global periodsDV "c._Iperiod_`i'y_1 c._Iperiod_`i'y_3 c._Iperiod_`i'y_4 c._Iperiod_`i'y_5  c._Iperiod_`i'y_6"
global convergenceperiods "c.germanexpshare1990#c._Iperiod_`i'y_1 c.germanexpshare1990#c._Iperiod_`i'y_3 c.germanexpshare1990#c._Iperiod_`i'y_4 c.germanexpshare1990#c._Iperiod_`i'y_5 c.germanexpshare1990#c._Iperiod_`i'y_6"
foreach v in lnexp lnexpplus1 asinhexp {
    local lbl`v' : variable label `v'
    eststo `v', title(`lbl`v''): qui reghdfe `v'_`i'y $periodsDV  c.treat2000#($periodsDV)   if insample_5y & !mi(period_`i'y) & period_5y>=1, a(product) cluster(product) old
}
foreach v in lnexp lnexpplus1 asinhexp {
    local lbl`v' : variable label `v'
    eststo `v'IV, title(`lbl`v''): qui reghdfe `v'_`i'y  $periodsDV  c.treat2000#c._Iperiod_`i'y_1   ///
        (c.treat2000#(c._Iperiod_`i'y_3 c._Iperiod_`i'y_4 c._Iperiod_`i'y_5  c._Iperiod_`i'y_6) =  c.treatIV#(c._Iperiod_`i'y_3 c._Iperiod_`i'y_4 c._Iperiod_`i'y_5  c._Iperiod_`i'y_6)) if insample_5y & !mi(period_`i'y) & period_5y>=1, a(product) cluster(product) old

    qui su treat2000 if e(sample)
    local treatsd = r(sd)
    matrix R`v' = J(1,3,.)
    qui su treat2000 if e(sample)
    local sdtreat = r(sd)
    local meantreat = r(mean)
    foreach dv of global periodsDV {
        local beta _b[`dv']
        matrix R`v' = R`v' \ [0, `beta',.] 
        qui lincom _b[c.treat2000#c.`dv']*`treatsd'                                          
        matrix R`v' = R`v' \ [1, r(estimate),r(se)]
    }
       
}

estout, $estout_params_IV_txt keep(c.treat2000*) mgroups("OLS" "2SLS", $estout_mgroups_ops) 
estout using "${path}tables/DIDyby.tex", replace $estout_params_IV $estout_pre $estout_post keep(c.treat2000*) mgroups("OLS" "2SLS", $estout_mgroups_ops) 
eststo clear

local missingperiod = 2
foreach v in lnexp lnexpplus1 asinhexp {
    preserve
    local lbl`v' : variable label `v'
    local lbl`v' = subinstr("`lbl`v''","$","",.)
    clear
    qui svmat R`v'
    drop if _n==1
    g period = floor((_n+1)/2)
    replace period = period+1 if period>=`missingperiod'
    qui tsset R`v'1 period
    qui tsfill
    qui replace R`v'2 = 0 if period==`missingperiod'
    //label define period_lbl 1 "1976-1980" 2 "1981-1985" 3 "1986-1990" 4 "1991-1995" 5 "1996-2000" 6 "2001-2005" 7 "2006-2010" 8 "2011-2015"
    label define period_lbl 1 "1985-1989" 2 "1990-1994" 3 "1995-1999" 4 "2000-2004" 5 "2005-2009" 6 "2010-2014", replace
    label val period period_lbl
    ren R`v'1 premium
    ren R`v'2 beta
    ren R`v'3 se
    g ui = beta + 1.96*se
    g li = beta - 1.96*se
    qui reshape wide beta se ui li, i(period) j(premium)
    ren beta1 beta
    ren se1 se
    ren ui1 ui
    ren li1 li
    g beta1 = beta0+beta
    g ui1 = beta0 + ui
    g li1 = beta0 + li
    twoway (scatter beta0 period, connect(direct) msymbol(square)) ///
        (scatter beta1 period, connect(direct) msymbol(triangle)), ///
            scheme(s1mono) legend(order(1 "treat=0" 2 "treat={&sigma}") symxsize(*.3)) ytitle("E(`v'|treat)") xtitle(Period) yline(0, lp(dot)) xline(2.5, lc(gs10)) title(`lbl`v'') xlabel(,valuelabel labsize(vsmall) angle(45))
    tempfile `v'
    graph save ``v''.gph, replace
    twoway (rcap ui li period, lc(black)) (scatter beta period, lc(red) lw(thin)), ///
             scheme(s1mono) legend(off) ytitle("{&beta}{sup:DID} (95% CI)") xtitle(Period) yline(0, lp(dot)) xline(2.5, lc(gs10)) xlabel(,valuelabel labsize(vsmall) angle(45))
    /*
    twoway (scatter beta period, connect(direct) lc(black) lw(thin) msymbol(circle)) ///
           (line ui period, lc(gs8) lp(dash)) (line li period, lc(gs8) lp(dash)), ///
             scheme(s1color) legend(off) ytitle("DID Estimator (95% CI)") xtitle(Period) yline(0, lp(dot)) xline(2.75, lc(gs10)) xlabel(,valuelabel labsize(vsmall) angle(45))
    */
    tempfile `v'2
    graph save ``v'2'.gph, replace
    restore
}

graph combine `lnexp'.gph `lnexpplus1'.gph `asinhexp'.gph, cols(3) scheme(s1color) //ycommon
//grc1leg `lnexp'.gph `lnexpplus1'.gph `asinhexp'.gph, cols(3) scheme(s1color) ycommon
graph export "${path}graphs/DIDyby.eps", replace as(eps)

graph combine `lnexp'.gph `lnexpplus1'.gph `asinhexp'.gph /// 
      `lnexp2'.gph `lnexpplus12'.gph `asinhexp2'.gph, rows(2) scheme(s1color) //ycommon
graph export "${path}graphs/DIDyby_both.eps", replace as(eps)

//B&W
graph combine `lnexp'.gph `lnexpplus1'.gph `asinhexp'.gph, cols(3) scheme(s1mono) //ycommon
//grc1leg `lnexp'.gph `lnexpplus1'.gph `asinhexp'.gph, cols(3) scheme(s1color) ycommon
graph export "${path}graphs/DIDyby_BW.eps", replace as(eps)

graph combine `lnexp'.gph `lnexpplus1'.gph `asinhexp'.gph /// 
      `lnexp2'.gph `lnexpplus12'.gph `asinhexp2'.gph, rows(2) scheme(s1mono) //ycommon
graph export "${path}graphs/DIDyby_both_BW.eps", replace as(eps)

/*********************************************************/
/* Tables exploring different occupation characteristics */
/*********************************************************/
//global typelist edu psf_amd task bf2
//order mig_rm_ls00_task_manual mig_rm_ls00_task_analytical

//Variable for number of workers (same as main result, for comparison purposes)
g mig_rm_ls00_mig_ct_1 = mig_rm_left_sam_2000

//Variable of workers weighted by wages, in thousands of Euros (marginal effect of 1000 euros of wages)
//g mig_rm_ls00_wageK_sm_1 = mig_rm_ls00_wage_sm/1000 //OLD VERSION
g mig_rm_ls00_wageK_sm_1 = mig_rm_ls00_lwage/1000
//g mig_rm_ls00_wageK_sm_1 = (mig_rm_ls00_durwsum/(mig_rm_ls00_durmain/30))/1000

//order mig_rm_ls00_wage_sm_0 mig_rm_ls00_wage_sm_1
//global typelist mig_ct wageK_sm edu task bf2 svct fwage_t25 wgr_cagr_amd
global typelist mig_ct edu task bf2 svct fwage_t25 wgr_cagr_amd 
qui ds *ls00*, v(32)
local vlist = r(varlist)
foreach v of local vlist {
    label var `v' "`v'"
}

label var mig_rm_ls00_mig_ct_1 "Total"
label var mig_rm_ls00_wageK_sm_1 "Wage ('000s)"
label var mig_rm_ls00_edu_low "Unskilled"
label var mig_rm_ls00_edu_hig "Skilled"
label var mig_rm_ls00_task_analytic_0 "Manual"
label var mig_rm_ls00_task_analytic_1 "Analytical/Cognitive"
label var mig_rm_ls00_bf2_higskill "High-skill Profession"
label var mig_rm_ls00_bf2_lowskill "Low-skill Profession"
label var mig_rm_ls00_svct_0 "Non-supervisor"
label var mig_rm_ls00_svct_1 "Supervisor"
label var mig_rm_ls00_fwage_t25_1 "High-paying firm"
label var mig_rm_ls00_fwage_t25_0 "Low-paying firm"
label var mig_rm_ls00_wgr_cagr_amd_1 "Fast growth wage"
label var mig_rm_ls00_wgr_cagr_amd_0 "Slow growth wage"


//SumStats Table
global sumstats_start \begin{threeparttable}[b] \begin{tabular}{l c c c c}\hline\hline \bf Variable  & \bf Mean & \bf sd & \bf Min & \bf Max \\ \hline
global sumstats_end \hline\end{tabular} \begin{tablenotes} \item {\footnotesize \tablenote} \end{tablenotes} \end{threeparttable}
/* open output file */
cap file close fh
file open fh using "${path}/tables/SumStatsOccupations.tex", write replace
/* write table header to file */
file write fh "$sumstats_start" _n
foreach i of global typelist {
    qui ds mig_rm_ls00_`i'_*, not(varl *mis*) v(32)
    local vlist = r(varlist)
    foreach v of local vlist {
        //local lab = substr("`v'",13,.)
        //local treatlab = subinstr("`lab'","_","",.)
        local treatlab: variable label `v' 
        qui su `v' if year==1995
        //local obs = `r(N)'
        local mean = `r(mean)'
        local se = `r(sd)'
        local min = `r(min)'
        local max = `r(max)'
        file write fh "`treatlab'" "&" %14.3fc (`mean')  "&" %14.2fc (`se')  "&" %14.1fc (`min')  "&" %14.1fc (`max') "\\"  _n
    }
    file write fh "\hline" _n
}

file write fh "$sumstats_end" _n
/* close file handle */
file close fh

//Table summarizing regressions for occupational groups
do "${path}do_files/OccupationsTable.do"

//Table with occupation characteristics for appendix
preserve
use "${path}data/mig_fyug_rm4_occ_v205.dta", clear
foreach v in kldb88_en task_2cat bf_2cat {
    decode `v', g(`v'_temp)
    g `v'_str =  subinstr(`v'_temp,word(`v'_temp,1),"",1)
}
keep if !mi(mig) & !mi(kldb88_en)

/*Extensive Margin*/
/* open output files */
cap file close fh
file open fh using "${path}tables/OccupationsList.tex", write replace
/* write table header to file*/
sort kldb88_en

local N = _N
forvalues i=1/`N' {
    qui levelsof kldb88_en_str if _n==`i', local(name) clean
    //local name = subinstr("`name'","&","\&",.)
    qui levelsof task_2cat_str if _n==`i', local(tasks) clean
    qui levelsof bf_2cat_str if _n==`i', local(skill) clean
    qui levelsof svct if _n==`i', local(supervisor) clean

    foreach v in tasks skill {
        if "``v''" == "" {
            local `v' = "n.a"
        }
    }        

    if "`supervisor'"=="" {
        local supervisor = "n.a"
    }
    else {
        local supervisor: di %9.2f `supervisor'
    }
    
    file write fh "`i'" "&" "`name'" "&" "`tasks'" "&" "`skill'" "&" "`supervisor'" "\\"  _n
}

//file write fh "\hline" _n
file close fh

/*
global sumstats_start \begin{threeparttable}[b] \begin{tabular}{l l l l l}\hline\hline  \bf Occupation & \bf Occurrences  & \bf Tasks & \bf Prof. skills & \bf Share supervisor \\\hline 
global sumstats_end  \hline \end{tabular} \begin{tablenotes} \item {\footnotesize \tablenote}  \end{tablenotes} \end{threeparttable}
use "${path}data/mig_fyug_rm4_occ_v205.dta", clear
keep if !mi(mig)
listtex kldb88_en mig task_2cat bf_2cat svct using "${path}tables/OccupationsList.tex", replace headlines("$sumstats_start") footlines("$sumstats_end")
*/
restore

/***************************/
/*** Knowledge intensity ***/
/***************************/
preserve
local y1min2 = $y1-2
local y2plus2 = $y2+2
egen temp1 = total($expdv) if year>=`y1min2' & year<=$y1, by(product)
egen temp2 = total($expdv) if year>=$y2 & year<=`y2plus2', by(product)
g cumexp = temp1/3 if year==$y1
replace cumexp = temp2/3 if year==$y2
drop temp1 temp2
replace lnexp = log(cumexp)
replace lnexpplus1 = log(cumexp+1)
replace asinhexp = asinh(cumexp)
foreach v in lnexp lnexpplus1 asinhexp {
    local lbl`v' : variable label `v'
    eststo, title(`lbl`v''): qui reghdfe `v'  c.treat2000#c.$after c.treat2000#c.$after#c.c_HCI if $didsample, $rhsoptions
}
foreach v in lnexp lnexpplus1 asinhexp {
    eststo, title(`lbl`v''): qui reghdfe `v'  (c.treat2000#c.$after c.treat2000#c.$after#c.c_HCI=c.treatIV#c.$after c.treatIV#c.$after#c.c_HCI)  if $didsample, $rhsoptions
}
estout, $estout_params_IV_txt mgroups("OLS" "2SLS", $estout_mgroups_ops) 
estout using "${path}tables/DIDMainCumExpProductHCI.tex", replace mgroups("OLS" "2SLS", $estout_mgroups_ops)  ///
	$estout_params_IV $estout_pre $estout_post  rename(c.treat2000#c.$after TreatmentXAfter c.treat2000#c.$after#c.c_HCI treatXafterXknowledgeint)
eststo clear
restore


/*******************/
/*** MORE GRAPHS ***/
/*******************/
g mig_rm_stay_sam_2000 = mig_rm_came_sam_9195-mig_rm_left_sam_2000

//Graph without points for slides
twoway (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995, msymbol(none)) (function y=x, range(mig_rm_came_sam_9195) lp(dash)), ///
       scheme(s1color) xtitle("Yugoslav workers arrived" "in sample b/t 91-95") ytitle("Yugoslav workers remaining" "in sample by 2000 or after")  legend(off) name(g1, replace)
graph export "${path}graphs/Migrants19902000_Empty.eps", as(eps) replace

twoway (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995, msymbol(none) mlabel(product) mlabsize(vsmall)) (function y=x, range(mig_rm_came_sam_9195) lp(dash)), ///
       scheme(s1color) xtitle("Yugoslav workers arrived" "in sample b/t 91-95") ytitle("Yugoslav workers remaining" "in sample by 2000 or after")  legend(off) name(g1, replace)
graph export "${path}graphs/Migrants19902000.eps", as(eps) replace

twoway (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==0,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==1,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==2,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==3,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==4,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==5,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==6,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==7,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==8,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==9,mlabel(product)) ///
       (function y=x, range(mig_rm_came_sam_9195) lp(dash) lc(black)), ///
       scheme(s1color)  xtitle("Yugoslav workers arrived" "in sample b/t 91-95") ytitle("Yugoslav workers remaining" "in sample by 2000 or after") ///
       legend(order(1 "Food & live animals" ///
                    2 "Beverages & tobacco" ///
                    3 "Crude materials, inedible, except fuels" ///
                    4 "Mineral fuels, lubricants & related materials" ///
                    5 "Animal and vegetable oils, fats & waxes" ///
                    6 "Chemical and related products, n.e.s." ///
                    7 "Manufactured goods classified by material" ///
                    8 "Machinery & transport equipment" ///
                    9 "Miscellaneous manufactured articles" ///
                    10 "Commodities & transactions not classified") cols(2) size(tiny) symx(*.25) symy(*.25) keygap(*.5)) ysize(8)
graph export "${path}graphs/Migrants19902000_1d.eps", as(eps) replace


twoway (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==0,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==1,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==2,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==3,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==4,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==5,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==6,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==7,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==8,mlabel(product)) ///
       (scatter mig_rm_stay_sam_2000 mig_rm_came_sam_9195 if year==1995 & product_1d==9,mlabel(product)) ///
       (function y=x, range(mig_rm_came_sam_9195) lp(dash) lc(black)), ///
       scheme(s1mono)  xtitle("Yugoslav workers arrived" "in sample b/t 91-95") ytitle("Yugoslav workers remaining" "in sample by 2000 or after") ///
       legend(order(1 "Food & live animals" ///
                    2 "Beverages & tobacco" ///
                    3 "Crude materials, inedible, except fuels" ///
                    4 "Mineral fuels, lubricants & related materials" ///
                    5 "Animal and vegetable oils, fats & waxes" ///
                    6 "Chemical and related products, n.e.s." ///
                    7 "Manufactured goods classified by material" ///
                    8 "Machinery & transport equipment" ///
                    9 "Miscellaneous manufactured articles" ///
                    10 "Commodities & transactions not classified") cols(2) size(tiny) symx(*.25) symy(*.25) keygap(*.5)) ysize(8)
graph export "${path}graphs/Migrants19902000_1d_BW.eps", as(eps) replace

twoway (scatter product mig_rm_left_sam_2000 if year==1995, msymbol(none) mlabel(product) mlabsize(vsmall)), ///
       scheme(s1color) xtitle("Treatment: workers in 91-95, out by 2000") legend(off) ylabel(none) ytitle("") name(g2, replace)

graph combine g1 g2, scheme(s1color)
//graph export "${path}graphs/Migrants19902000.eps", as(eps) replace

graph box treat2000, over(product_1d, label(labsize(vsmall) angle(45))) ytitle("Treatment (asinh)") scheme(s1color)
graph export "${path}/graphs/TreatmentBoxByIndustry.eps", replace as(eps)	
 
preserve
xtile treat_qt = treat2000, n(4)
collapse (sum) exp_xdeu, by(year treat_qt)
//collapse (sum) exppc_xdeu, by(year treat_qt)
format %14.0g exp_xdeu
//outsheet using "${path}data/DataQuartileGraph.csv", comma replace noq

replace exp_xdeu = exp_xdeu/1000000
//replace exppc_xdeu = exppc_xdeu/1000000

sort treat_qt year

twoway (line $expdv year if treat_qt==1, lp(dash_dot)) (line $expdv year if treat_qt==2, lp(dash) lw(medthick)) ///
       (line $expdv year if treat_qt==3, lw(medthick)) (line $expdv year if treat_qt==4, lw(thick)),  ///
       scheme(s1color) legend(order(1 "1st quartile" 2 "2nd quartile" 3 "3rd quartile" 4 "4th quartile") rows(2)) ///
       ytitle("Cumulative exports USD, millions") xtitle(Year) xlab(1985(5)2015, labsize(small)) xline(1995, lp(dot) lc(red)) 
graph export "${path}graphs/TreatExports_yby.eps", as(eps) replace

twoway (line $expdv year if treat_qt==1, lp(dash_dot) lc(white)) (line $expdv year if treat_qt==2, lp(dash) lw(medthick) lc(white)) ///
       (line $expdv year if treat_qt==3, lw(medthick) lc(white)) (line $expdv year if treat_qt==4, lw(thick) lc(white)),  ///
       scheme(s1color) legend(order(1 "1st quartile" 2 "2nd quartile" 3 "3rd quartile" 4 "4th quartile") rows(2)) ///
       ytitle("Cumulative exports USD, millions") xtitle(Year) xlab(1985(5)2015, labsize(small)) xline(1995, lp(dot) lc(red)) 
graph export "${path}graphs/TreatExports_yby_Slides1.eps", as(eps) replace

twoway (line $expdv year if treat_qt==1, lp(dash_dot)) (line $expdv year if treat_qt==2, lp(dash) lw(medthick) lc(white)) ///
       (line $expdv year if treat_qt==3, lw(medthick) lc(white)) (line $expdv year if treat_qt==4, lw(thick) lc(white)),  ///
       scheme(s1color) legend(order(1 "1st quartile" 2 "2nd quartile" 3 "3rd quartile" 4 "4th quartile") rows(2)) ///
       ytitle("Cumulative exports USD, millions") xtitle(Year) xlab(1985(5)2015, labsize(small)) xline(1995, lp(dot) lc(red)) 
graph export "${path}graphs/TreatExports_yby_Slides2.eps", as(eps) replace

twoway (line $expdv year if treat_qt==1, lp(dash_dot)) (line $expdv year if treat_qt==2, lp(dash) lw(medthick)) ///
       (line $expdv year if treat_qt==3, lw(medthick)  lc(white)) (line $expdv year if treat_qt==4, lw(thick)  lc(white)),  ///
       scheme(s1color) legend(order(1 "1st quartile" 2 "2nd quartile" 3 "3rd quartile" 4 "4th quartile") rows(2)) ///
       ytitle("Cumulative exports USD, millions") xtitle(Year) xlab(1985(5)2015, labsize(small)) xline(1995, lp(dot) lc(red)) 
graph export "${path}graphs/TreatExports_yby_Slides3.eps", as(eps) replace

twoway (line $expdv year if treat_qt==1, lp(dash_dot)) (line $expdv year if treat_qt==2, lp(dash) lw(medthick)) ///
       (line $expdv year if treat_qt==3, lw(medthick)) (line $expdv year if treat_qt==4, lw(thick)  lc(white)),  ///
       scheme(s1color) legend(order(1 "1st quartile" 2 "2nd quartile" 3 "3rd quartile" 4 "4th quartile") rows(2)) ///
       ytitle("Cumulative exports USD, millions") xtitle(Year) xlab(1985(5)2015, labsize(small)) xline(1995, lp(dot) lc(red)) 
graph export "${path}graphs/TreatExports_yby_Slides4.eps", as(eps) replace

twoway (line $expdv year if treat_qt==1, lp(dash_dot)) (line $expdv year if treat_qt==2, lp(dash) lw(medthick)) ///
       (line $expdv year if treat_qt==3, lw(medthick)) (line $expdv year if treat_qt==4, lw(thick)),  ///
       scheme(s1color) legend(order(1 "1st quartile" 2 "2nd quartile" 3 "3rd quartile" 4 "4th quartile") rows(2)) ///
       ytitle("Cumulative exports USD, millions") xtitle(Year) xlab(1985(5)2015, labsize(small)) xline(1995, lp(dot) lc(red)) 
graph export "${path}graphs/TreatExports_yby_Slides5.eps", as(eps) replace

//B&W
twoway (line $expdv year if treat_qt==1, lp(dash_dot)) (line $expdv year if treat_qt==2, lp(dash) lw(medthick)) ///
       (line $expdv year if treat_qt==3, lw(medthick)) (line $expdv year if treat_qt==4, lw(thick)),  ///
       scheme(s1mono) legend(order(1 "1st quartile" 2 "2nd quartile" 3 "3rd quartile" 4 "4th quartile") rows(2)) ///
       ytitle("Cumulative exports USD, millions") xtitle(Year) xlab(1985(5)2015, labsize(small)) xline(1995, lp(dot) lc(red)) 
graph export "${path}graphs/TreatExports_yby_BW.eps", as(eps) replace

/*
g asinhexp = exp_xdeu
twoway (line asinhexp year if treat_qt==1, lp(dash_dot)) (line asinhexp year if treat_qt==2, lp(dash) lw(medthick)) ///
       (line asinhexp year if treat_qt==3, lw(medthick)) (line asinhexp year if treat_qt==4, lw(thick)),  ///
       scheme(s1color) legend(order(1 "1st quartile" 2 "2nd quartile" 3 "3rd quartile" 4 "4th quartile") rows(2)) ///
       ytitle("Yearly exports USD, millions (asinh)") xtitle(Year) xlab(1985(5)2015, labsize(small)) xline(1995, lp(dot) lc(red)) 
graph export "${path}graphs/TreatExports_asinh_yby.eps", as(eps) replace
*/

restore

/*******************/
/*** CDK DATASET ***/
/*******************/
do "${path}do_files/DiffInDiffCDK.do"

/*********************************/
/*** UNIDO AND BOSNIA DATASETS ***/
/*********************************/
do "${path}do_files/DiffInDiffUNIDOandBosnia.do"

/*********************/
/*** PLACEBO TESTS ***/
/*********************/
do "${path}do_files/SyntheticControls.do"
//do "${path}do_files/DiffInDiffPlacebos.do"

/******************************/
/*** APPENDIX TABLES/GRAPHS ***/
/******************************/
do "${path}do_files/DiffInDiffAppendix.do"
//do "${path}do_files/DiffInDiffW93.do"
do "${path}do_files/AllTables.do"

